package com.yzh;

import java.util.Arrays;

/**
 * @author 于振晗
 * @title: AddArray
 * @description: TODO 数组扩容
 * @date 2020年08月19日
 * @path com.yzh
 */
public class AddArray {
    public static void main(String[] args) {
        int sum=0;
        for(int i=1;i<10;i++){
            do{
                i++;
                if(i%2!=0)
                    sum+=i;
            }while(i<6);
            System.out.println(sum);
        }

        System.out.println(sum);

    }
    //正向迭代
    public static void positiveIteration1(){
        int [] arr = {5,6,4,7,8,2,1};
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }
    }
    public static void positiveIteration2(){
        int [] arr = {5,6,4,7,8,2,1};
        for (int i : arr) {
            System.out.println(i);
        }

    }
    public static void positiveIteration3(){
        int [] arr = {5,6,4,7,8,2,1};
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }

    }

    /**
     * 逆向迭代
     */
    public static void reverseIteration1(){
        int [] arr = {5,6,4,7,8,2,1};
        for (int i = arr.length - 1; i >= 0; i--) {
            System.out.println(arr[i]);
        }
    }
    public static void reverseIteration2(){
        int [] arr = {5,6,4,7,8,2,1};
        for (int i = arr.length - 1; i >= 0; i--) {
            System.out.println(arr[i]);
        }

    }
    public static void reverseIteration3(){
        int [] arr = {5,6,4,7,8,2,1};
        for (int i = arr.length - 1; i >= 0; i--) {
            System.out.println(arr[i]);
        }

    }
    /**
     * 数组更新操作
     */
    public static void updArr1(int [] arr,int index,int value){
        if (arr[index] == value) {
            System.out.println("无需更新");
        }else{
            arr[index] = value;
        }
    }
    public static void updArr2(int [] arr,int index,int value){
        if (arr[index] == value) {
            System.out.println("无需更新");
        }else{
            arr[index] = value;
        }
    }

    public static void updArr3(int [] arr,int index,int value){
        if (arr[index] == value) {
            System.out.println("无需更新");
        }else{
            arr[index] = value;
        }
    }
    /**
     * 扩容操作
     */
    public static int [] addArr1(int [] arr){
        if(arr[arr.length-1] != 0){
            arr = Arrays.copyOf(arr,arr.length+(arr.length>>1));
        }
        System.out.println(Arrays.toString(arr));
        return arr;
    }
    public static int [] addArr2(int [] arr){
        if(arr[arr.length-1] != 0){
            arr = Arrays.copyOf(arr,arr.length+(arr.length>>1));
        }
        System.out.println(Arrays.toString(arr));
        return arr;
    }
    public static int [] addArr3(int [] arr){
        if(arr[arr.length-1] != 0){
            arr = Arrays.copyOf(arr,arr.length+(arr.length>>1));
        }
        System.out.println(Arrays.toString(arr));
        return arr;
    }
}
